/*
-------------------------------------------------

	Texture.cpp

	Dmitriy Kuzmenko (Dgut)

-------------------------------------------------
*/

#include "Texture.h"
#include "PNG.h"

namespace Autumn
{

/*
-------------------------------------------------

	Texture

-------------------------------------------------
*/

Texture::Texture( void ) :
	id	( 0 )
{
	glGenTextures( 1, &id );
}

Texture::~Texture( void )
{
	glDeleteTextures( 1, &id );
}

/*
-------------------------------------------------

	Texture2D

-------------------------------------------------
*/

Texture2D::Texture2D( void )
{
}

Texture2D::~Texture2D( void )
{
}

void Texture2D::Image( GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid * pixels )
{
	Bind();
	glTexImage2D( GetTarget(), level, internalFormat, width, height, 0, format, type, pixels );
}

void Texture2D::Image( const PNG * png )
{
	const GLenum FORMAT = png->GetType() & PNG_COLOR_MASK_ALPHA ? GL_RGBA : GL_RGB;
	Image( 0, FORMAT, png->GetWidth(), png->GetHeight(), FORMAT, GL_UNSIGNED_BYTE, png->GetData() );
}

Texture2D * const Texture2D::CreateStandart( const char * fileName )
{
	Texture2D * texture = new Texture2D();
	texture->Image( &PNG( fileName ) );
	texture->Parameter( GL_TEXTURE_MIN_FILTER, GL_LINEAR/*/GL_NEAREST*/ );
	texture->Parameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR/*/GL_NEAREST*/ );
	texture->Parameter( GL_TEXTURE_WRAP_S, /*GL_REPEAT/*/GL_CLAMP_TO_EDGE );
	texture->Parameter( GL_TEXTURE_WRAP_T, /*GL_REPEAT/*/GL_CLAMP_TO_EDGE );

	return texture;
}

}	// namespace Autumn